class Group < ActiveRecord::Base

	def self.authorization(loginId, kontroller, aktion)
		return true if loginId == "admin"
		ugroups = getGroupFromLogin(loginId)
		aktionId = getActionId(kontroller, aktion)
		ugroups.each{|ugroup|
			authorize = Groupacl.find(:first, :conditions=>["group_id = ? AND action_id = ?", ugroup.group_id, aktionId])
			if !authorize.nil?
				if authorize.permission == 1
					return true
				end
			end
		}
		return false
	end
	
	def self.getGroupFromLogin(login)
		user = User.find(:first, :conditions=>["login = ?", login])
		ugroups = Usergroup.find(:all, :conditions=>["user_id = ?", user.id])
		return ugroups
	end
	def self.getActionId(kontrol, aktion)
		actionId = Groupaction.find(:first, :conditions=>["controller = ? AND action = ?", kontrol, aktion])
		return actionId
	end

end
